// (c) Copyright 2014 Stephen Gold <sgold@sonic.net>
// Distributed under the terms of the GNU General Public License

/*
 This file is part of the Bats Game.

 The Bats Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by the
 Free Software Foundation, either version 3 of the License, or (at your
 option) any later version.

 The Bats Game is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.

 You should have received a copy of the GNU General Public License
 along with the Bats Game.  If not, see <http://www.gnu.org/licenses/>.
 */
package bats.control.physics;

import bats.control.BatsControl;
import bats.scene.WorldNode;
import bats.unit.UnitClass;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import java.util.logging.Logger;
import jme3utilities.Validate;
import jme3utilities.physics.BubbleControl;

/**
 * A bubble control triggered by an unimpaired unit.
 * <p>
 * Each instance is disabled at creation.
 *
 * @author Stephen Gold <sgold@sonic.net>
 */
public class BatsBubbleControl
        extends BubbleControl {
    // *************************************************************************
    // constants

    /**
     * message logger for this class
     */
    final private static Logger logger =
            Logger.getLogger(BatsBubbleControl.class.getName());
    // *************************************************************************
    // constructors

    /**
     * Instantiate a disabled control.
     *
     * @param initialShape initial collision shape for the object (not null)
     * @param physicsSpace which physics space will contain the object (not
     * null)
     * @param successor control to be enabled when the object is touched, or
     * null for none
     */
    public BatsBubbleControl(CollisionShape initialShape,
            PhysicsSpace physicsSpace, Control successor) {
        super(initialShape, physicsSpace, successor);
        assert !isEnabled();
    }
    // *************************************************************************
    // BubbleControl methods

    /**
     * Enable the successor.
     *
     * @param successor which control to enable (not null)
     */
    @Override
    protected void enableSuccessor(Control successor) {
        Validate.nonNull(successor, "successor control");
        BatsControl.setEnabled(successor, true);
    }

    /**
     * Test whether the specified object is eligible to pop the bubble.
     *
     * @param object which object (not null)
     * @return true if eligible, false if ineligible
     */
    @Override
    protected boolean isEligible(Spatial object) {
        Validate.nonNull(object, "object");

        WorldNode world = WorldNode.find(spatial);
        UnitClass units = world.getUnitClass(object);
        boolean result = units != null && !units.isImpaired(object);

        return result;
    }
}